package cn.silver.module.web.service;


import cn.silver.framework.core.exception.ServiceException;
import cn.silver.framework.core.utils.SpringUtils;
import cn.silver.module.authority.bean.ClientBean;
import cn.silver.module.web.model.LoginModel;
import cn.silver.module.web.model.LoginResultModel;

/**
 * 授权策略
 *
 * @author Silver Zhou
 */
public interface IAuthStrategy {

    String BASE_NAME = "AuthStrategy";

    /**
     * 登录
     */
    static LoginResultModel login(ClientBean client, LoginModel loginModel) {
        // 授权类型和客户端id
        String grantType = loginModel.getGrantType();
        String beanName = grantType + BASE_NAME;
        if (!SpringUtils.containsBean(beanName)) {
            throw new ServiceException("授权类型不正确!");
        }
        IAuthStrategy instance = SpringUtils.getBean(beanName);
        instance.validate(loginModel);
        return instance.doLogin(client, loginModel);
    }

    /**
     * 参数校验
     */
    void validate(LoginModel loginModel);

    /**
     * 登录
     */
    LoginResultModel doLogin(ClientBean client, LoginModel loginModel);

}
